//resource为检查对象，icon为图标，destination为显示提示符的地方，warnword提示错误信息,size是字符串长度。
//当text获取焦点时显示的类容
function getfocus(icon,destination,warnword)   {
	icon.className = 'warn';
	destination.innerHTML = warnword;
	destination.style.color="black";
}
//检查是否字符串的长度正确
function ischarsizeright(resource,minsize,maxsize,icon,destination,warnword,type)   {
	var str = resource.value;
	if((str.length == maxsize)&&(type == 1))
	{
		icon.className = 'right';
		destination.innerHTML = "";
	}
	else if((str.length >= minsize)&&(str.length <= maxsize)&&(type == 2))
	{
		icon.className = 'right';
		destination.innerHTML = "";
	}
	else
	{
		icon.className = 'error';
		destination.style.color="red";
		destination.innerHTML = warnword;
	}
}
//判读email是否正确；
function checkemail(resource,icon,destination,warnword){
	var newPar=/[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+$/;
	var strInteger = resource.value;
	if(newPar.test(strInteger))
	{
		icon.className = 'right';
		destination.innerHTML = "";
	}
	else
	{
		icon.className = 'error';
		destination.style.color="red";
		destination.innerHTML = warnword;
	}
 }
 //判读确认密码是否与新密码相同
 function checkpsswd(resource,resource2,icon,destination,warnword){
	if((resource.value == resource2.value)&&((resource2.value).length >= 6))
	{
		icon.className = 'right';
		destination.innerHTML = "";
	}
	else
	{
		icon.className = 'error';
		destination.style.color="red";
		destination.innerHTML = warnword;
	}
 }
 
 //检查书本ISBN
 function checkisbn(resource,icon,destination,warnword)
{
	var isbn= resource.value;//获取isbn
	if(isbn.length != 17)
	{
		icon.className = 'error';
		destination.style.color="red";
		destination.innerHTML = warnword;
		return false;
	}
	
	var regexp=/\d{3,3}\-\d{1,5}\-\d{2,5}\-\d{1,6}\-([0-9])+$/;
	if (!regexp.test(isbn))
	{
		icon.className = 'error';
		destination.style.color="red";
		destination.innerHTML = warnword;
		return false;
	}
	var regexSp0='';
	var regexSp1='';
	var regexSp2='';
	var regexSp3='';
	var regexSp4='';
	var i;
	var bit = isbn.indexOf('-',0);
	regexSp4 = isbn.substring(0, bit);
	var oldbit = bit +1;
	bit = isbn.indexOf('-',bit+1);
	regexSp0 = isbn.substring(oldbit, bit);
	oldbit = bit+1;
	bit = isbn.indexOf('-',bit+1);
	regexSp1 = isbn.substring(oldbit, bit);
	oldbit = bit+1;
	bit = isbn.indexOf('-',bit+1);
	regexSp2 = isbn.substring(oldbit, bit);
	regexSp3 = isbn.substring(16);
	
    var check=0;//检验数
    var j = 1;
   	
   	for(i=0;i < regexSp4.length;i++)
    {
    	var num = regexSp4.substring(i,i + 1);
    	if(j%2 == 0)
        	check=check - -3*num;
        else
        	check=check - -num;
        j++;
    }
   	
    for(i=0;i < regexSp0.length;i++)
    {
    	var num = regexSp0.substring(i,i + 1);
    	if(j%2 == 0)
        	check=check - -3*num;
        else
        	check=check - -num;
        j++;
    }
    for(i=0;i<regexSp1.length;i++)
    {
       var num = regexSp1.substring(i,i + 1);
    	if(j%2 == 0)
        	check=check - -3*num;
        else
        	check=check - -num;
        j++;
    }
    
    for(i=0;i<regexSp2.length;i++)
    {
        var num = regexSp2.substring(i,i + 1);
    	if(j%2 == 0)
        	check=check - -3*num;
        else
        	check=check - -num;
        j++;
    }
   
    var last = regexSp3;
    if(((10 - check%10))%10 != regexSp3) 
    {
    	//alert(check);
    	icon.className = 'error';
		destination.style.color="red";
		destination.innerHTML = warnword;
		return false;
    }
    icon.className = 'right';
	destination.innerHTML = "";
	return true;
}

//检查提交信息
//size表示测试的项目个数；
function changecheck(size)   {
	var mark = 0;//标记是否有错误信息；
	var markchage = 0;//未修改元素个数
	var i;
	var icon;
	for(i = 1; i <= size;i++)
	{
		icon = document.getElementById("icon_" + i);
		if(icon.className == "error")
		{
			mark = 1;
			break;
		}
		else if(icon.className == "write")
		{
			markchage = markchage - -1;
		}
	}
	if(mark == 1)//如果有错误
	{
		alert("Information fill in errors!");
		return false;
	}
	
	if(markchage == size)//如果没有修改就提交
	{
		alert("Nothing is fill!");
		return false;
	}
	
	return true;
	
}

//
//size表示测试的项目个数；添加的测试；
function check(size)   {
	var mark = 0;//标记是否有错误信息；
	var i;
	var icon;
	for(i = 1; i <= size;i++)
	{
		icon = document.getElementById("icon_" + i);
		if(icon.className != "right")
		{
			mark = 1;
			break;
		}
	}
	if(mark == 1)
	{
		alert("Information fill in errors!");
		return false;
	}
	return true;
	
}

//检查是否为正数 
function isUnsignedNumeric(resource,icon,destination,warnword)   { 
	var newPar = /^\d+(\.\d+)?$/;
	var strNumber = resource.value;
	if(newPar.test(strNumber))
	{
		icon.className = 'right';
		destination.innerHTML = "";
	}
	else
	{
		icon.className = 'error';
		destination.style.color="red";
		destination.innerHTML = warnword;
	}
} 
//检查是否为正整数 
function isUnsignedInteger(resource,icon,destination,warnword)   { 
	var newPar=/^\d+$/;
	var strInteger = resource.value;
	if(newPar.test(strInteger))
	{
		icon.className = 'right';
		destination.innerHTML = "";
	}
	else
	{
		icon.className = 'error';
		destination.style.color="red";
		destination.innerHTML = warnword;
	}
}

//时间超过系统时间测试测试；
function CheckOutTime(year,month,day)
{
	var y,m,d;
	//获取系统时间；
	var date=new Date();
	y = date.getYear();
	m = date.getMonth() + 1;
	d = date.getDate();
	if(year > y)
		return false;
	else if(year == y)
	{
		if(month > m)
			return false;
		else if(month == m)
		{
			if(day > d)
				return false;
		}
	}
	return true;
}
//检查生日中的时间是否正确；
function CheckDay(year,month,day)
{
	var check;
	var y = year;
	var m = month;
	var d = day;
	if((m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8) || (m == 10) || (m == 12))
	{
		if((d <= 0) || (d > 31))
		{
			return false;
		}
	}
	else if((m == 4) || (m == 6) || (m == 9) || (m == 11))
	{
		if((d <= 0) || (d > 30))
		{
			return false;
		}

	}
	else if(m == 2)
	{
		if(y%4==0 && y%100!=0 || y%400==0)//判断是是闰年；
		{
			if((d <= 0) || (d > 29))
			{
				return false;
			}
		}
		else//判断是平年；
		{
			if((d <= 0) || (d > 28))
			{
				return false;
			}
		}
	}
	else
	{
		return false;
	}
	return true;
}


//检查时间是否正确 
function checkdate(resource,icon,destination,warnword)
{
	var date = resource.value;
	var datestyle=/\d{4,4}\-\d{1,2}\-\d{1,2}$/;
	if (!datestyle.test(date))
	{
		icon.className = 'error';
		destination.style.color="red";
		destination.innerHTML = warnword;
		return false;
	}
	var year,month,day;
	//获取年份
	var bit = date.indexOf('-',0);
	year = date.substring(0, bit);
	//获取月份
	var oldbit = bit +1;
	bit = date.indexOf('-',bit+1);
	month = date.substring(oldbit, bit);
	//获取日期
	oldbit = bit+1;
	day = date.substring(oldbit);
	
	if(!CheckOutTime(year,month,day))
	{
		icon.className = 'error';
		destination.style.color="red";
		destination.innerHTML = warnword;
		return false;
	}
	
	if(!CheckDay(year,month,day))
	{
		icon.className = 'error';
		destination.style.color="red";
		destination.innerHTML = warnword;
		return false;
	}
	
	icon.className = 'right';
	destination.innerHTML = "";
	return true;
}
//由年月确定日的天数；
function ensurnday(year,month,day)
{
	var check;
	var y = year.options[year.options.selectedIndex].value;
	var m = month.options[month.options.selectedIndex].value;
	if((m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8) || (m == 10) || (m == 12))
	{
		if(day.options.length <31)
		{
			for(var i = day.options.length;i < 31;i++)
				day.options.add(new Option(i+1,i+1)); 
		}
	}
	else if((m == 4) || (m == 6) || (m == 9) || (m == 11))
	{
		if(day.options.length <30)
		{
			for(var i = day.options.length;i < 30;i++)
				day.options.add(new Option(i+1,i+1)); 
		}
		else if(day.options.length >30)
		{
			for(var i = day.options.length;i > 30;i--)
				day.options.remove(i - 1); 
		}
	}
	else if(m == 2)
	{
		if(y%4==0 && y%100!=0 || y%400==0)//判断是是闰年；
		{
			if(day.options.length <29)
			{
				for(var i = day.options.length;i < 29;i++)
					day.options.add(new Option(i+1,i+1)); 
			}
			else if(day.options.length >29)
			{
				for(var i = day.options.length;i > 29;i--)
					day.options.remove(i - 1); 
			}
		}
		else//判断是平年；
		{
			if(day.options.length <28)
			{
				for(var i = day.options.length;i < 28;i++)
					day.options.add(new Option(i+1,i+1)); 
			}
			else if(day.options.length >28)
			{
				for(var i = day.options.length;i > 28;i--)
					day.options.remove(i - 1); 
			}
		}
	}
	else
	{
		return false;
	}
}

//检查是否为正整数
function checkid(resource,size,icon,destination,warnword)   { 
	if((resource.value).length != size)
	{
		icon.className = 'error';
		destination.style.color="red";
		destination.innerHTML = warnword;
		return;
	}
	var newPar=/^\d+$/;
	var strInteger = resource.value;
	if(newPar.test(strInteger))
	{
		icon.className = 'right';
		destination.innerHTML = "";
	}
	else
	{
		icon.className = 'error';
		destination.style.color="red";
		destination.innerHTML = warnword;
	}
}

//检查查询id
function checksearchid(resource,size,icon,destination,warnword){ 
	if((resource.value).length == 0)
	{
		icon.className = 'right';
		destination.innerHTML = "";
		return;
	}
	checkid(resource,size,icon,destination,warnword);
}

//检查查询书本ISBN
 function checksearchisbn(resource,icon,destination,warnword)
{
	var isbn= resource.value;//获取isbn
	if(isbn.length == 0)
	{
		icon.className = 'right';
		destination.innerHTML = "";
		return true;
	}
	return checkisbn(resource,icon,destination,warnword);
}

//检查查询phone
function checkPhone(resource,minsize,maxsize,icon,destination,warnword) 
{ 
	if((resource.value.length < minsize)&&(resource.value.length > maxsize))
	{
		icon.className = 'error';
		destination.style.color="red";
		destination.innerHTML = warnword;
		return false;  
	}
	
	if(resource.value.length == 0)
	{
		icon.className = 'right';
		destination.innerHTML = "";
		return true;
	}
	
	var t = /^\d+$/; //区号-号码 
	if(!t.test(resource.value)){ 
		icon.className = 'error';
		destination.style.color="red";
		destination.innerHTML = warnword;
		return false; 
	}
	 
	icon.className = 'right';
	destination.innerHTML = "";
	return true;
}